﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Restaurant.DAO
{
    public class LoaiNguyenLieuDB
    {
        private SqlConnection conn = KetNoiDB.TaoKetNoi();

        public DataTable LayLoaiNguyenLieu()
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayTatCaLoaiNL";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayLoaiNguyenLieuTheoMa(int ma)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayLoaiNguyenLieuTheoMa";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayDonViTinh()
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayTatCaDonViTinh";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayDonViTinhTheoMa(int ma)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayDonViTinhTheoMa";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool ThemLoaiNguyenLieu(string TenLoaiNL)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spThemLoaiNguyenLieu";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@tenLoaiNguyenLieu", TenLoaiNL));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool ThemDonViTinh(string tenDVTinh)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spThemDonViTinh";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@tenDonViTinh", tenDVTinh));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaLoaiNguyenLieu(int maLoaiNL)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spXoaLoaiNguyenLieu";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maLoaiNL", maLoaiNL));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaDonViTinh(int maDVTinh)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spXoaDonViTinh";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maDonViTinh", maDVTinh));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SuaLoaiNguyenLieu(int maLoaiNL, string tenLoaiNL)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spSuaLoaiNguyenLieu";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maLoaiNL", maLoaiNL));
                cmd.Parameters.Add(new SqlParameter("@tenLoaiNL", tenLoaiNL));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SuaDonViTinh(int ma, string ten)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spSuaDonViTinh";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maDVtinh", ma));
                cmd.Parameters.Add(new SqlParameter("@tenDonViTinh", ten));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
